<?php
/**
 * Created by PhpStorm.
 * User: wangwenfan
 * Date: 2018/8/2
 * Time: 15:52.
 */

namespace App\Admin\Controllers;

use App\Models\Base\LogVipRank;
use App\Models\Base\ServerList;
use App\Service\ChannelService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class VipUserRankController extends Controller
{
    /**首页
     * @return \Encore\Admin\Layout\Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.data_statistic'));
            $content->description(trans('game.vip_user_rank'));
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        $grid = Admin::grid(LogVipRank::class, function (Grid $grid) {
            $get = request()->all();
            $type = isset($get['type']) ? $get['type'] : 0;
            if (!$type) {
                $grid->model()->where('type', 1);
            }
            $grid->server()->name(trans('game.server_name'));
            $grid->channel()->name(trans('game.channel'));
            $grid->name(trans('game.role_name'));
            $grid->vip_level(trans('game.vip_lv'))->sortable();
            $grid->military(trans('game.fight_power'))->sortable();
            $grid->surplus_gold(trans('game.gold'))->sortable();
            $grid->amount(trans('game.amount'))->sortable();
            $grid->profession(trans('game.profession
            '))->display(function ($profession) {
                return getProfession($profession);
            });
            $grid->no_pay_days(trans('game.not_charged_day'));
            $grid->last_pay_time(trans('game.recent_time'));
            $grid->no_login_days(trans('game.no_login_days'));
            $grid->last_login_time(trans('game.last_login_date'));
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
            $grid->filter(function ($filter) use ($type) {
                $filter->disableIdFilter();
                $params = request()->all();
                if (isset($params['channel_id']) && 99 == $params['channel_id']) {
                    $filter->setNotWhere(['channel_id']);
                }
                $filter->equal('server_id', trans('game.server_name'))->select(ServerList::all()->pluck('name', 'server_id'));
                $filter->equal('channel_id', trans('game.channel'))->select(ChannelService::getChannelList());
            });
        });

        return $grid;
    }

    protected function filterRankScript()
    {
        $str = <<<EOF
            var channel_id  = $(".channel_id").parents('.form-group');
            var server_id = $(".server_id").parents('.form-group');
            channel_id.hide();
            $(".type").parents('.radio-inline').on('click',function(){
                var type = $(this).find('.type').val();
                if(type == 1){
                    channel_id.hide();
                    server_id.show();
                }else if(type == 2){
                    channel_id.show();
                    server_id.show();
                }
            });
EOF;
        Admin::script($str);
    }
}
